package test.dev.code;

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        int[] nums ={2,7,11,15};
        int target = 9;
        int[] ints = twoSum(nums, target);
        System.out.println(ints);

    }

    public static int[] twoSum(int[] nums, int target) {

        // hash表储存 {nums[i], i}
        HashMap<Integer, Integer> hmap = new HashMap<>();
        int[] res = new int[2];

        for(int i = 0; i < nums.length; i++){
            if((!hmap.isEmpty()) && hmap.containsKey(target - nums[i])){
                res[0] = i;
                res[1] = hmap.get(target - nums[i]);
            }
            hmap.put(nums[i], i);
        }

        return res;

    }


}